package rpc;

import com.hoppinzq.service.common.HoppinInvocationRequest;
import com.hoppinzq.service.common.HoppinInvocationResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class RpcClientHandler extends SimpleChannelInboundHandler<HoppinInvocationResponse> {
    private final CompletableFuture<HoppinInvocationResponse> future = new CompletableFuture<>();
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, HoppinInvocationResponse response) throws Exception {
        future.complete(response);
    }
    public Object getResponse() throws InterruptedException, ExecutionException {
        return future.get().getResult();
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}